package com.googlecode.tvlistings;

import java.text.SimpleDateFormat;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public final class ProgramListAdapter extends ArrayAdapter<TVProgram> {
	@SuppressWarnings("unused")
	private Activity context;
	private List<TVProgram> tvPrograms;
	private LayoutInflater mInflater;

	public ProgramListAdapter(Activity context, List<TVProgram> tvPrograms) {
		super(context, R.layout.program_list_item, tvPrograms);

		this.context = context;
		this.tvPrograms = tvPrograms;
		mInflater = context.getLayoutInflater();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemViewHolder itemViewHolder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.program_list_item, parent, false);
			itemViewHolder = new ItemViewHolder();
			itemViewHolder.prTitle = (TextView) convertView.findViewById(R.id.prog_title);
			itemViewHolder.prTime = (TextView) convertView.findViewById(R.id.prog_time);
			itemViewHolder.chIcon = (ImageView) convertView.findViewById(R.id.chan_icon);
			itemViewHolder.chTitle = (TextView) convertView.findViewById(R.id.chan_title);
			convertView.setTag(itemViewHolder);
		} else {
			itemViewHolder = (ItemViewHolder) convertView.getTag();
		}

		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
		itemViewHolder.prTime.setText(timeFormat.format(tvPrograms.get(position).pTime));
		itemViewHolder.prTitle.setText(tvPrograms.get(position).pName);
		itemViewHolder.chTitle.setText(tvPrograms.get(position).cTitle);
		itemViewHolder.chIcon.setImageResource(tvPrograms.get(position).cIcon);
		
		return convertView;
	}

	private class ItemViewHolder {
		public TextView prTitle;
		public TextView prTime;
		public ImageView chIcon;
		public TextView chTitle;
	}
}
